﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;

namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JChuJiaGongWuListAdd : PageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "JChuJiaGongWuListEasyUI";
            }
        }

        private List<JChuJiaTempFlowInfo> _listFlowTemp = null;
        protected List<JChuJiaTempFlowInfo> listFlowTemp
        {
            get {
                if (_listFlowTemp == null)
                    _listFlowTemp = JChuJiaGongWuListController.GetAllJChuJiaTempFlow();
                return _listFlowTemp;
            }            
        }



        protected void InitBind()
        {
            //管制物料
            List<JChuJiaItemDetailInfo> listDetail = new List<JChuJiaItemDetailInfo>();
            for(int i=0;i<10;i++)
            {
                listDetail.Add(new JChuJiaItemDetailInfo());
            }

            List<JChuJiaSerialDetailInfo> listSerial = new List<JChuJiaSerialDetailInfo>();
            for (int i = 0; i < 20; i++)
            {
                listSerial.Add(new JChuJiaSerialDetailInfo());
            }

            this.rplistDetail.DataSource = listDetail;
            this.rplistDetail.DataBind();

            this.rplistSerial.DataSource = listSerial;
            this.rplistSerial.DataBind();

            rpFlowTemp.DataSource = listFlowTemp;
            rpFlowTemp.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }

            InitBind();
        }

        private void addmenu()
        {
            JChuJiaGongWuListInfo obj = Request.SerializableObject<JChuJiaGongWuListInfo>();

            char[] caSplit = new char[] { ',' };
            //管制零件
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] Notes = GetRequestValue<string>("Note", "").Split(caSplit);
            //承办人
            string[] ChuFlowTempIds = GetRequestValue<string>("ChuFlowTempId", "").Split(caSplit);
            string[] DeptTitles = GetRequestValue<string>("DeptTitle", "").Split(caSplit);
            string[] UserIds = GetRequestValue<string>("UserId", "").Split(caSplit);
            string[] RealNames = GetRequestValue<string>("RealName", "").Split(caSplit);
            string[] CBDates = GetRequestValue<string>("CBDate", "").Split(caSplit);
            string[] CBNotes = GetRequestValue<string>("CBNote", "").Split(caSplit);
            string[] CBResults = GetRequestValue<string>("CBResult", "").Split(caSplit);
            string[] IsSends = GetRequestValue<string>("IsSend", "").Split(caSplit);
            
            //管制机号
            string[] JHSerialNumbers = GetRequestValue<string>("JHSerialNumber", "").Split(caSplit);
            string[] SJSerialNumbers = GetRequestValue<string>("SJSerialNumber", "").Split(caSplit);
            string[] SerialNotes = GetRequestValue<string>("SerialNote", "").Split(caSplit);

             //管制零件
            List<JChuJiaItemDetailInfo> _listItem = new List<JChuJiaItemDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                JChuJiaItemDetailInfo _itmItem = new JChuJiaItemDetailInfo();


                _itmItem.ItemCode = ItemCodes[rowIndex];
                if (string.IsNullOrEmpty( _itmItem.ItemCode))
                    continue;

                _itmItem.ItemName = ItemNames[rowIndex];
                _itmItem.Quantity = Convert.ToInt32(Quantitys[rowIndex]);
                _itmItem.Note = Notes[rowIndex];
                _listItem.Add(_itmItem);
            }
            //承办人
            List<JChuJiaFlowDetailInfo> _listFlow = new List<JChuJiaFlowDetailInfo>();
            for (int rowIndex = 0; rowIndex < ChuFlowTempIds.Count(); rowIndex++)
            {
                JChuJiaFlowDetailInfo _itmItem = new JChuJiaFlowDetailInfo();
                int ChuFlowTempId = -1;
                try
                {
                    ChuFlowTempId = Convert.ToInt32(ChuFlowTempIds[rowIndex]);
                }
                catch
                {
                    ChuFlowTempId = -1;
                }

                if (ChuFlowTempId == -1)
                    continue;

                _itmItem.ChuFlowTempId = ChuFlowTempId;
                _itmItem.DeptTitle = DeptTitles[rowIndex];
                _itmItem.UserId = Convert.ToInt32(UserIds[rowIndex]);
                _itmItem.RealName = RealNames[rowIndex];
                _itmItem.CBDate = CBDates[rowIndex];
                _itmItem.CBNote = CBNotes[rowIndex];
                _itmItem.CBResult = CBResults[rowIndex];
                _itmItem.IsSend = Convert.ToBoolean(IsSends[rowIndex]);
                _listFlow.Add(_itmItem);

                //创建人
                if (obj.AddUserId <= 0)
                {
                    obj.AddUserId = _itmItem.UserId;
                    obj.AddRealName = _itmItem.RealName;
                }

            }
            //管制机号
            List<JChuJiaSerialDetailInfo> _listSerial = new List<JChuJiaSerialDetailInfo>();
            for (int rowIndex = 0; rowIndex < JHSerialNumbers.Count(); rowIndex++)
            {
                JChuJiaSerialDetailInfo _itmItem = new JChuJiaSerialDetailInfo();
                _itmItem.JHSerialNumber = JHSerialNumbers[rowIndex];
                _itmItem.SJSerialNumber = SJSerialNumbers[rowIndex];
                _itmItem.Note = SerialNotes[rowIndex];
                if (string.IsNullOrEmpty(_itmItem.JHSerialNumber) && string.IsNullOrEmpty(_itmItem.SJSerialNumber) && string.IsNullOrEmpty(_itmItem.Note))
                    continue;
                _listSerial.Add(_itmItem);
            }

            int r=JChuJiaGongWuListController.Add(obj, _listItem, _listSerial, _listFlow);

            if (r > 0)
                ShowTopSuccessJson("已成功发布初加工物管制票，并邮件通知下个承办人。");
            else
                ErrorJson("发布失败,错误代码:"+r.ToString());

        }
    }
}